* Create experience measures for all cardiologists in the data

clear all
set more off
pause on

set more off
foreach i in 7 6 5 4 3 2 1{

	if `i'==1{
	local finyear "0910"
	local year "2009"
	}
	
	if `i'==2{
	local finyear "0809"
	local year "2008"
	}
	
	if `i'==3{
	local finyear "0708"
	local year "2007"
	}
	
	if `i'==4{
	local finyear "0607"
	local year "2006"
	}
	
	if `i'==5{
	local finyear "0506"
	local year "2005"
	}
	
	if `i'==6{
	local finyear "0405"
	local year "2004"
	}
	
	if `i'==7{
	local finyear "0304"
	local year "2003"
	}
	
	if `i'==7{
	local finyear "0304"
	local year "2003"
	}
	
	
use fulldiag3_* procode* epikey tretspef mainspef opertn* using "$rawdata/ip_`year'", clear

* Ordering doesn't matter here - should be unique, if it's not it's due to critical care variables that I don't use (all other info is common)
bys epikey: gen x=_n
keep if x==1
drop x

merge 1:1 epikey using "$consids/id_`finyear'"
keep if _merge==3
drop _merge

gen ami = 0
replace ami = 1 if substr(fulldiag3_1,1,3)=="I21"
replace ami = 1 if substr(fulldiag3_1,1,3)=="I22"

gen trust_code=substr(procode,1,3)

* Want the data for those who are consultants in 2005 - 2017 period
merge m:1 pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==1

* Now count patients who are 1) AMI, 2) Cardio, 3) Stented 4) All diagnoses 5) Stemi
gen cardio = 0
replace cardio = 1 if tretspef=="320" | mainspef=="320"

gen total = 1 if _merge==3

#delimit ;
****************2*******************;
******Extracting Operations*********;
************************************;

****Stents*****;

gen first_stent = 0;
replace first_stent = 1 if substr(opertn_01,1,3)=="K75";
replace first_stent = 1 if opertn_01=="K491" & opertn_02=="Y022";

gen any_k491=0;
gen any_y022 = 0;
gen any_k75 = 0;

local i = 1;
while `i'<=9{;
cap confirm str variable opertn_0`i';
if !_rc{;
replace any_k491 = 1 if opertn_0`i'=="K491";
replace any_y022 = 1 if opertn_0`i'=="Y022";
replace any_k75 = 1 if substr(opertn_0`i',1,3)=="K75";
};
else{;
drop opertn_0`i';
};
local i = `i'+1;
};

cap gen opertn_13="";

local i = 10;
while `i'<=24{;
cap confirm str variable opertn_`i';
if !_rc{;
replace any_k491 = 1 if opertn_`i'=="K491";
replace any_y022 = 1 if opertn_`i'=="Y022";
replace any_k75 = 1 if substr(opertn_`i',1,3)=="K75";
};
else{;
drop opertn_`i';
};
local i = `i'+1;
};

gen any_stent = 0;
replace any_stent = 1 if (any_k491==1 & any_y022==1)|(any_k75==1);


******PCI*******;

gen first_pci= 0;
replace first_pci = 1 if substr(opertn_01,1,3)=="K75";
replace first_pci = 1 if opertn_01=="K491" & opertn_02=="Y022";

label variable first_pci "First operation is a PCI";

gen any_pci=0;


local i = 1;
while `i'<=9{;
cap confirm str variable opertn_0`i';
if !_rc{;
replace any_pci = 1 if opertn_0`i'=="K484"|substr(opertn_0`i',1,3)=="K49"|opertn_0`i'=="K501"|substr(opertn_0`i',1,3)=="K75";
};
else{;
};
local i = `i'+1;
};


local i = 10;
while `i'<=24{;
cap confirm str variable opertn_`i';
if !_rc{;
replace any_pci = 1 if opertn_`i'=="K484"|substr(opertn_`i',1,3)=="K49"|opertn_`i'=="K501"|substr(opertn_`i',1,3)=="K75";

};
else{;
};
local i = `i'+1;
};

#delimit cr

gen pciami = 0
replace pciami = 1 if any_pci==1 & ami==1

gen stentami = 0
replace stentami = 1 if any_stent==1 & ami==1

gen stemi = 0

foreach x in I210 I211 I212 I213 I220 I221{
replace stemi=1 if fulldiag3_1=="`x'"
}

gen finyear=`year'

bys pconsult trust_code: gen x=1 if _n==1
egen number_hosps = sum(x), by(pconsult)

collapse (sum) ami total cardio any_pci first_pci any_stent first_stent pciami stentami stemi (max) number_hosps, by(pconsult finyear)

save "$savedata/experience_`year'.dta", replace
}


* 2010 - 2015 PERIOD HERE

set more off
forval year=2016(1)2017{
	
use diag_* procode* epikey tretspef mainspef opertn* pconsult using "$rawdata/ip_`year'", clear

bys epikey: gen x=_n
keep if x==1
drop x

gen ami = 0
replace ami = 1 if substr(diag_01,1,3)=="I21"
replace ami = 1 if substr(diag_01,1,3)=="I22"

gen trust_code=substr(procode,1,3)

* Want the data for those who are consultants in 2010 - 2015 period
merge  m:1  pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==1

* Now count patients who are 1) AMI, 2) Cardio, 3) Stented 4) All diagnoses
gen cardio = 0
replace cardio = 1 if tretspef=="320" | mainspef=="320"

gen total = 1 if _merge==3

#delimit ;
****************2*******************;
******Extracting Operations*********;
************************************;

****Stents*****;

gen first_stent = 0;
replace first_stent = 1 if substr(opertn_01,1,3)=="K75";
replace first_stent = 1 if opertn_01=="K491" & opertn_02=="Y022";

gen any_k491=0;
gen any_y022 = 0;
gen any_k75 = 0;

local i = 1;
while `i'<=9{;
cap confirm str variable opertn_0`i';
if !_rc{;
replace any_k491 = 1 if opertn_0`i'=="K491";
replace any_y022 = 1 if opertn_0`i'=="Y022";
replace any_k75 = 1 if substr(opertn_0`i',1,3)=="K75";
};
else{;
drop opertn_0`i';
};
local i = `i'+1;
};

cap gen opertn_13="";

local i = 10;
while `i'<=24{;
cap confirm str variable opertn_`i';
if !_rc{;
replace any_k491 = 1 if opertn_`i'=="K491";
replace any_y022 = 1 if opertn_`i'=="Y022";
replace any_k75 = 1 if substr(opertn_`i',1,3)=="K75";
};
else{;
drop opertn_`i';
};
local i = `i'+1;
};

gen any_stent = 0;
replace any_stent = 1 if (any_k491==1 & any_y022==1)|(any_k75==1);


******PCI*******;

gen first_pci= 0;
replace first_pci = 1 if substr(opertn_01,1,3)=="K75";
replace first_pci = 1 if opertn_01=="K491" & opertn_02=="Y022";

label variable first_pci "First operation is a PCI";

gen any_pci=0;


local i = 1;
while `i'<=9{;
cap confirm str variable opertn_0`i';
if !_rc{;
replace any_pci = 1 if opertn_0`i'=="K484"|substr(opertn_0`i',1,3)=="K49"|opertn_0`i'=="K501"|substr(opertn_0`i',1,3)=="K75";
};
else{;
};
local i = `i'+1;
};


local i = 10;
while `i'<=24{;
cap confirm str variable opertn_`i';
if !_rc{;
replace any_pci = 1 if opertn_`i'=="K484"|substr(opertn_`i',1,3)=="K49"|opertn_`i'=="K501"|substr(opertn_`i',1,3)=="K75";

};
else{;
};
local i = `i'+1;
};

#delimit cr

gen pciami = 0
replace pciami = 1 if any_pci==1 & ami==1

gen stentami = 0
replace stentami = 1 if any_stent==1 & ami==1

gen stemi = 0

foreach x in I210 I211 I212 I213 I220 I221{
replace stemi=1 if substr(diag_01,1,4)=="`x'"
}

gen finyear=`year'

bys pconsult trust_code: gen x=1 if _n==1
egen number_hosps = sum(x), by(pconsult)

collapse (sum) ami total cardio any_pci first_pci any_stent first_stent pciami stentami stemi (max) number_hosps, by(pconsult finyear)

save "$savedata/experience_`year'.dta", replace
}

set more off
forval year=2010(1)2015{
	
use fulldiag3_* procode* epikey tretspef mainspef opertn* pconsult using "$rawdata/ip_`year'", clear

bys epikey: gen x=_n
keep if x==1
drop x

gen ami = 0
replace ami = 1 if substr(fulldiag3_1,1,3)=="I21"
replace ami = 1 if substr(fulldiag3_1,1,3)=="I22"

gen trust_code=substr(procode,1,3)

* Want the data for those who are consultants in 2010 - 2015 period
merge  m:1  pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==1

* Now count patients who are 1) AMI, 2) Cardio, 3) Stented 4) All diagnoses
gen cardio = 0
replace cardio = 1 if tretspef=="320" | mainspef=="320"

gen total = 1 if _merge==3

#delimit ;
****************2*******************;
******Extracting Operations*********;
************************************;

****Stents*****;

gen first_stent = 0;
replace first_stent = 1 if substr(opertn_01,1,3)=="K75";
replace first_stent = 1 if opertn_01=="K491" & opertn_02=="Y022";

gen any_k491=0;
gen any_y022 = 0;
gen any_k75 = 0;

local i = 1;
while `i'<=9{;
cap confirm str variable opertn_0`i';
if !_rc{;
replace any_k491 = 1 if opertn_0`i'=="K491";
replace any_y022 = 1 if opertn_0`i'=="Y022";
replace any_k75 = 1 if substr(opertn_0`i',1,3)=="K75";
};
else{;
drop opertn_0`i';
};
local i = `i'+1;
};

cap gen opertn_13="";

local i = 10;
while `i'<=24{;
cap confirm str variable opertn_`i';
if !_rc{;
replace any_k491 = 1 if opertn_`i'=="K491";
replace any_y022 = 1 if opertn_`i'=="Y022";
replace any_k75 = 1 if substr(opertn_`i',1,3)=="K75";
};
else{;
drop opertn_`i';
};
local i = `i'+1;
};

gen any_stent = 0;
replace any_stent = 1 if (any_k491==1 & any_y022==1)|(any_k75==1);


******PCI*******;

gen first_pci= 0;
replace first_pci = 1 if substr(opertn_01,1,3)=="K75";
replace first_pci = 1 if opertn_01=="K491" & opertn_02=="Y022";

label variable first_pci "First operation is a PCI";

gen any_pci=0;


local i = 1;
while `i'<=9{;
cap confirm str variable opertn_0`i';
if !_rc{;
replace any_pci = 1 if opertn_0`i'=="K484"|substr(opertn_0`i',1,3)=="K49"|opertn_0`i'=="K501"|substr(opertn_0`i',1,3)=="K75";
};
else{;
};
local i = `i'+1;
};


local i = 10;
while `i'<=24{;
cap confirm str variable opertn_`i';
if !_rc{;
replace any_pci = 1 if opertn_`i'=="K484"|substr(opertn_`i',1,3)=="K49"|opertn_`i'=="K501"|substr(opertn_`i',1,3)=="K75";

};
else{;
};
local i = `i'+1;
};

#delimit cr

gen pciami = 0
replace pciami = 1 if any_pci==1 & ami==1

gen stentami = 0
replace stentami = 1 if any_stent==1 & ami==1

gen stemi = 0

foreach x in I210 I211 I212 I213 I220 I221{
replace stemi=1 if fulldiag3_1=="`x'"
}

gen finyear=`year'

bys pconsult trust_code: gen x=1 if _n==1
egen number_hosps = sum(x), by(pconsult)

collapse (sum) ami total cardio any_pci first_pci any_stent first_stent pciami stentami stemi (max) number_hosps, by(pconsult finyear)

save "$savedata/experience_`year'.dta", replace
}

*/

set more off
use "$savedata/experience_2003.dta", clear

forval year=2004(1)2017{
append using "$savedata/experience_`year'.dta"
}

encode pconsult, gen(consid)

xtset consid finyear

* Patient numbers over various years
foreach var in ami total cardio any_pci first_pci any_stent first_stent pciami stentami stemi{
gen past_`var' = .
replace past_`var' = L.`var' if finyear>2003

gen past5_`var' = .
replace past5_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' if finyear>2008

gen pastever_`var' = .

replace pastever_`var' = L.`var' if finyear==2004
replace pastever_`var' = L.`var'+L2.`var' if finyear==2005
replace pastever_`var' = L.`var'+L2.`var'+L3.`var' if finyear==2006
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var' if finyear==2007
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' if finyear==2008
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var' if finyear==2009
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var' if finyear==2010
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var'+L8.`var' if finyear==2011
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var'+L8.`var'+L9.`var' if finyear==2012
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var'+L8.`var'+L9.`var'+L10.`var' if finyear==2013
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var'+L8.`var'+L9.`var'+L10.`var'+L11.`var' if finyear==2014
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var'+L8.`var'+L9.`var'+L10.`var'+L11.`var'+L12.`var' if finyear==2015
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var'+L8.`var'+L9.`var'+L10.`var'+L11.`var'+L12.`var'+L13.`var' if finyear==2016
replace pastever_`var' = L.`var'+L2.`var'+L3.`var'+L4.`var'+L5.`var' +L6.`var'+L7.`var'+L8.`var'+L9.`var'+L10.`var'+L11.`var'+L12.`var'+L13.`var'+L14.`var' if finyear==2017

}

* Years as a consultant

gen firstyear = .

forval year=2003(1)2017{
gen temp = total if finyear==`year'
egen temp2 = max(temp), by(pconsult)

replace firstyear = `year' if temp2!=0 & firstyear==.
drop temp temp2
}

foreach var in total ami cardio any_pci first_pci any_stent first_stent pciami stentami{

gen firstyear_`var' = .

forval year=2003(1)2017{
gen temp = `var' if finyear==`year'
egen temp2 = max(temp), by(pconsult)

replace firstyear_`var' = `year' if temp2!=0 & firstyear_`var'==.
drop temp temp2
}

}

xtset consid finyear
gen pastmulti = .
replace pastmulti = 0 if L.num==1
replace pastmulti = 1 if L.num>1

gen multi = .
replace multi = 0 if num==1
replace multi = 1 if num>1

gen perc_pci = pciami / ami
gen perc_stent = stentami / ami
gen perc_pci_all = any_pci / total

foreach x in 1 2 3 4 5{
gen L`x'perc_pci = L`x'.perc_pci
gen L`x'perc_stent = L`x'.perc_stent
}


foreach var in ami cardio total stemi{
gen past3_`var' = L.`var'+L2.`var'+L3.`var'
}

gen new_cons = 0
replace new_cons = 1 if finyear==firstyear

foreach var in ami cardio total{
gen new_cons_`var' = 0
replace new_cons_`var' = 1 if finyear==firstyear_`var'
}


gen ami3 = ami+L.ami+L2.ami
gen cardio3 = cardio + L.cardio + L2.cardio
gen total3 = total + L.total + L2.total
gen stemi3 = stemi+L.stemi+L2.stemi

foreach var in ami cardio total{
gen l_`var' = L.`var'
gen l2_`var' = L2.`var'
}

gen share_ami = ami/total
gen l_share_ami = l_ami/l_total
gen l2_share_ami = l2_ami/l2_total


save "$savedata/cardio_experience0317.dta", replace


